<?php


namespace App\Utils\Notice;


use App\Constants\Model\Notice as NoticeConstants;
use App\Dao\CooperativeUserDao;
use App\Model\NoticeCooperative;
use Hyperf\DbConnection\Model\Model;
use Hyperf\Di\Annotation\Inject;

class CooperativeReceivedTaskPush extends Notice
{

    /**
     * @Inject()
     * @var CooperativeUserDao
     */
    protected $cooperativeUserDao;

    public $model = NoticeCooperative::class;

    public $messageTemplate = '您好，官方向您推送了一条商机，请注意查收';

    //前端路由-商机-列表页
    public $routeName = 'BusinessPush';

    public $data;

    public function toDatabase()
    {
        //发送给合作商 = 发送给合作商的所有运营人员 这里需要把合作商转化为运营人员
        $cooperativeUserIds = $this->cooperativeUserDao->where(['cooperative_id' => $this->receiverId])->pluck('id')->toArray();

        foreach ($cooperativeUserIds as $cooperativeUserId) {
            $data = $this->format($this->data);
            $data['cooperative_user_id'] = $cooperativeUserId;
            /** @var Model $model */
            $model = new $this->model;
            $model->fill($data)->save();
        }
    }

    public function format(array $data)
    {
        $return = [];
        //定义通知的类型
        $return['type'] = NoticeConstants::COOPERATIVE_RECEIVED_TASK_PUSH;
        //扩展字段 跳转到-商机页
        $return['extension'] = [
            'path' => $this->routeName
        ];
        //接受者id
        $return['cooperative_user_id'] = $data['cooperative_user_id'];

        $return['content'] = $this->messageTemplate;

        return $return;
    }
}